/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.Message;
import Beans.User;
import Mail.Mail;
import dao.MessageDao;
import dao.UserDao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.joda.time.LocalDate;

/**

 @author Marie-Claude
 */
@ManagedBean(name = "userListManager")
@ViewScoped
public class UserListManager implements Serializable {

  private List<User> lstUsers;
  private int idUserToLook;
  private String usernameUserToLook;
  @ManagedProperty(value = "#{userManager}")
  private UserManager mbUser;
  private String usernameSearch;
  private String citySearch;
  private String sexSearch;
  private String ageSearch;
  private ArrayList<Integer> lstSelect = new ArrayList();//select list de l'age
  private boolean isData = false;
  private boolean isMessage = false;
  private User user;
  private String sendToUsername;
  private Message message;

  /**
   Creates a new instance of UserListManager
   */
  public UserListManager() {
  }

  public List<User> getLstUsers() {
    return lstUsers;
  }

  public void setLstUsers(List<User> lstUsers) {
    this.lstUsers = lstUsers;
  }

  public int getIdUserToLook() {
    return idUserToLook;
  }

  public void setIdUserToLook(int idUserToLook) {
    this.idUserToLook = idUserToLook;
  }

  public String getUsernameUserToLook() {
    return usernameUserToLook;
  }

  public void setUsernameUserToLook(String usernameUserToLook) {
    this.usernameUserToLook = usernameUserToLook;
  }

  public UserManager getMbUser() {
    return mbUser;
  }

  public void setMbUser(UserManager mbUser) {
    this.mbUser = mbUser;
  }

  public String getUsernameSearch() {
    return usernameSearch;
  }

  public void setUsernameSearch(String usernameSearch) {
    this.usernameSearch = usernameSearch;
  }

  public String getCitySearch() {
    return citySearch;
  }

  public void setCitySearch(String citySearch) {
    this.citySearch = citySearch;
  }

  public String getSexSearch() {
    return sexSearch;
  }

  public void setSexSearch(String sexSearch) {
    this.sexSearch = sexSearch;
  }

  public String getAgeSearch() {

    return ageSearch;
  }

  public void setAgeSearch(String ageSearch) {
    this.ageSearch = ageSearch;
  }

  public ArrayList<Integer> getLstSelect() {
    return lstSelect;
  }

  public void setLstSelect(ArrayList<Integer> lstSelect) {
    this.lstSelect = lstSelect;
  }

  public boolean isIsData() {
    return isData;
  }

  public void setIsData(boolean isData) {
    this.isData = isData;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  public String getSendToUsername() {
    return sendToUsername;
  }

  public void setSendToUsername(String sendToUsername) {
    this.sendToUsername = sendToUsername;
  }

  public boolean isIsMessage() {
    return isMessage;
  }

  public void setIsMessage(boolean isMessage) {
    this.isMessage = isMessage;
  }

  public Message getMessage() {
    return message;
  }

  public void setMessage(Message message) {
    this.message = message;
  }

  @PostConstruct
  public void init() {
    user = new User();
    for (int i = 18; i <= 100; i += 6) {//initialisation du select menu pour la recherche avec l'age
      lstSelect.add(i);
    }
  }

  public String search() {
    FacesMessage globalMsg;
    HashMap monMap = new HashMap();
    if (usernameSearch != null) {
      monMap.put("usernameSearch", "%" + usernameSearch + "%");
    }
    if (citySearch != null) {
      monMap.put("citySearch", "%" + citySearch + "%");
    }
    if (sexSearch != null) {
      monMap.put("sexSearch", sexSearch);
    }

    if (ageSearch != null) {// Search sur l'age
      if (ageSearch.equals("select")) {
        monMap.put("ageSearch", null);//Si l'age est égal au string 'select', on met à null 
      } else {
        LocalDate now = new LocalDate();//sinon, on prend l'année actuelle et on soustrait l'age choisi
        int currentYear = now.getYear();
        int yearInBd = currentYear - Integer.parseInt(ageSearch);

        monMap.put("ageSearch", yearInBd);//la soustraction retourne l'année de naissance minimum à retourner de la bd
      }
    }
    isData = false;
    lstUsers = UserDao.selectAllSearch(monMap);
    if (lstUsers.isEmpty() && lstUsers != null) {
      globalMsg = new FacesMessage("No results found");
      FacesContext.getCurrentInstance().addMessage(null, globalMsg);
    }
    return null;
  }

  public String lookToProfile(int userId, String username) {
    isData = true;
    idUserToLook = userId;

    user = UserDao.getUserById(idUserToLook);
    return null;
  }

  public String sendMessage(String username) {
    sendToUsername = username;
    isMessage = true;

    return null;
  }

  public String sendM(int userId) {
    message.setSenderID(mbUser.getUser().getUserID());
    message.setRecipientID(userId);
    MessageDao.insertMessage(message);

    Mail.envoyerMail("tpjava3@gmail.com", "Message reçu par search!", "Vous avez reçu un nouveau message de la part de ");

    return null;
  }

  public String close() {
    isData = false;
    return null;
  }
}
